package com.probiz.estoresf.sales.web.action;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.cart.util.ShoppingCartUtil;
import com.probiz.estore.common.model.cart.Shoppingcart;
import com.probiz.estore.common.model.cart.ShoppingcartItemGc;
import com.probiz.estore.core.controller.GenericStoreFrontController;

public class GiftCertificateFrontController extends GenericStoreFrontController {
	
	@Override
	protected void initController() throws Exception {
	}
	
	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		ShoppingcartItemGc shoppingcartItemGc = new ShoppingcartItemGc(); 
		request.setAttribute("shoppingcartItemGc", shoppingcartItemGc);
		return new ModelAndView("sales/buyGiftCertificate");
	}
	

	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) {
		putItByShoppingCart(request);
		return new ModelAndView("redirect:/cart/shoppingcart.html");
	}
	
	public ModelAndView saveAndCheckout(HttpServletRequest request, HttpServletResponse response) {
		putItByShoppingCart(request);
		return new ModelAndView("redirect:/checkout.html");  
	}

	
	/**
	 * 把赠送礼券存放在购物车中;
	 * @param request
	 */
	private void putItByShoppingCart(HttpServletRequest request) {
		ShoppingcartItemGc shoppingcartItemGc = new ShoppingcartItemGc();
		String giftCertAmt = request.getParameter("giftCertAmt");
		String purchaser = request.getParameter("purchaser");
		String message = request.getParameter("message");
		String recipient = request.getParameter("recipient");
		String recipientEmail = request.getParameter("recipientEmail");
		shoppingcartItemGc.setGiftCertAmt(new BigDecimal(giftCertAmt));
		shoppingcartItemGc.setPurchaser(purchaser);
		shoppingcartItemGc.setMessage(message);
		shoppingcartItemGc.setRecipient(recipient);
		shoppingcartItemGc.setRecipientEmail(recipientEmail);
		
		Shoppingcart shoppingcart = ShoppingCartUtil.getInstance().getCurrentUserShoppingcart();
		shoppingcart.addGiftCertificateItem(shoppingcartItemGc);
	}

}
